----------------------Global variables-------------------
if (g_SPI_init == nil ) then
	g_SPI_init = true
end

if (g_SPI_nBus == nil) then
	g_SPI_nBus = 0
end

--[[
-- Use default mode and frequnecy unless user specific it! --
if (g_SPI_nBusMode == nil) then
	g_SPI_nBusMode = eSPIModeCPHA1CPOL1
end

--
-- eRTSPIFreq400KHz = (1<<0),
-- eRTSPIFreq200KHz = (1<<1),
-- eRTSPIFreq100KHz = (1<<2),
-- eRTSPIFreq83KHz = (1<<3),
-- eRTSPIFreq71KHz = (1<<4),
-- eRTSPIFreq62KHz = (1<<5),
-- eRTSPIFreq50KHz = (1<<6),
-- eRTSPIFreq25KHz = (1<<7),
-- eRTSPIFreq10KHz = (1<<8),
-- eRTSPIFreq5KHz = (1<<9),
-- eRTSPIFreq2KHz = (1<<10),
-- eRTSPIFreq1MHz = (1<<11),
-- eRTSPIFreq2MHz = (1<<12),
-- eRTSPIFreq3MHz = (1<<13),
-- eRTSPIFreq5MHz = (1<<14),
-- eRTSPIFreq6MHz = (1<<15),
-- eRTSPIFreq10MHz = (1<<16),
-- eRTSPIFreq12MHz = (1<<17),
-- eRTSPIFreq15MHz = (1<<18),
-- eRTSPIFreq20MHz = (1<<19),
-- eRTSPIFreq24MHz = (1<<20),
-- eRTSPIFreq36MHz = (1<<21),
-- eRTSPIFreq40MHz = (1<<22),
-- eRTSPIFreq60MHz = (1<<23),
-- eRTSPIFreqCustom = (1<<30),
-- eRTSPIFreqUnknow = (1<<31),
--

if (g_SPI_nFrequencyMode == nil) then
	g_SPI_nFrequencyMode = eRTSPIFreq10MHz
end

if (g_SPI_nFrequency == nil) then
	g_SPI_nFrequency = 10 * 1000000
end
--]]


if (g_SPI_bCSActiveLow == nil) then
	g_SPI_bCSActiveLow = true
end

if (g_SPI_nBus == nil) then
	g_SPI_Bus = 0
end




---------------------------------------------------------------------------------------
-- Write
---------------------------------------------------------------------------------------

function spi_w_nbyte(data)
	local nResult, bRet, nCmd, length

	nCmd = 0
	length = #data
	--nResult = RTBB_SPIHLWriteCS( g_hBridgeBoard, g_SPI_nBus, g_SPI_nCSPin, g_SPI_nRegSize, length, nCmd, data)
	nResult = RTBB_SPIHLWriteCS( g_hBridgeBoard, g_SPI_nBus, g_SPI_nCSPin, 0, length, nCmd, data)
	bRet = resultMsg("RTBB_SPIHLWriteCS", nResult)

	dbgprintf("SPI Write: ")
	for i=1, #data, 1 do
		dbgprintf("0x%x,", data[i])
	end
	dbgprintf("\n")

	return bRet
end


function SPIW(cmd)

	if (type(cmd) == "table") then
		return spi_w_nbyte( cmd )
	else
		array[1] = cmd
		return spi_w_nbyte( array )
	end
end

---------------------------------------------------------------------------------------
-- READ
---------------------------------------------------------------------------------------

function spi_r_nbyte(cmd, nReadLength, nCmdLength)

	local data = {}
	local nResult
	local nCmd
	local bRet

	nCmd = 0

	nResult = RTBB_SPIHLReadCS( g_hBridgeBoard, g_SPI_nBus, g_SPI_nCSPin, nCmdLength, nReadLength, cmd, data )
	bRet = resultMsg( "RTBB_SPIHLReadCS", nResult )

	dbgprintf("SPI Read: CMD = 0x%08X, CMD length = %d, data = ", cmd, nCmdLength)
	for i=1, #data, 1 do
		dbgprintf("0x%x,", data[i])
	end
	dbgprintf("\n")

	return data, bRet;
end


function SPIR(cmd,nReadLength, nCmdLength)
	if (nReadLength == nil) then nReadLength = 1 end
	if (type(cmd) == "table") then
		local i, nCmd
		local cmd_len
		nCmd = 0
		cmd_len = #cmd
		if (cmd_len > 4) then
			cmd_len = 4
		end
		for i = 1, cmd_len, 1 do
			nCmd = nCmd * 256 + cmd[i]
		end
		return spi_r_nbyte(nCmd, nReadLength, cmd_len)
	else
		if (nCmdLength==nil) then nCmdLength = 1 end
		return spi_r_nbyte(cmd, nReadLength, nCmdLength)
	end
end



---------------------------------------------------------------------------------------
-- Init SPI module
---------------------------------------------------------------------------------------
function init_module_SPI()

	local nResult
	local nFrequency
	local hEnum
	local info
	local strBoardName

	hEnum = RTBB_EnumBoard();

	if (hEnum ~= nil) then
		info = RTBB_GetEnumBoardInfo(hEnum, 0)
		strBoardName = RTBB_BIGetBoardName(info)
		if( string.find( strBoardName, "Eagleboard") ~= nil ) then
			if (g_SPI_nCSPin==nil) then g_SPI_nCSPin = 16 end
		else
			if( string.find( strBoardName, "Sparrow") ~= nil) then
				if (g_SPI_nCSPin==nil) then g_SPI_nCSPin = 2 end
			else
				-- Wrenboard does not support SPI communication
				RTBB_FreeEnumBoard(hEnum)
				return false
			end
		end
		RTBB_FreeEnumBoard(hEnum)
	else
		return false
	end

	if (g_SPI_nBusMode ~= nil) then
		nResult = RTBB_SPISetMode( g_hBridgeBoard, g_SPI_nBus, g_SPI_nBusMode)
		if (nResult ~= RT_BB_SUCCESS) then
			return false --errMsg("RTBB_SPISetMode", nResult)
		end
	end
	

	if (g_SPI_nFrequencyMode ~= nil) then
		nResult = RTBB_SPISetFrequency(g_hBridgeBoard, g_SPI_nBus, g_SPI_nFrequencyMode, g_SPI_nFrequency)
		if (nResult ~= RT_BB_SUCCESS) then
			return false --errMsg("RTBB_SPISetFrequency", nResult)
		end
	end

end
